package com.srit.base.ibatis.handler;

import java.sql.SQLException;

import com.ibatis.sqlmap.client.extensions.ParameterSetter;
import com.ibatis.sqlmap.client.extensions.ResultGetter;
import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback;

/**
 * @author guan<br>
 *         数据库 char 与 java boolean匹配方法
 * 
 */
public class YesNoTypeHandlerCallback implements TypeHandlerCallback {

	@Override
	public void setParameter(ParameterSetter setter, Object parameter)
			throws SQLException {
		if (parameter == null) {
			setter.setString("");
		} else {
			if (parameter instanceof Boolean) {
				Boolean b = (Boolean) parameter;
				if (b.booleanValue()) {
					setter.setString("Y");
				} else {
					setter.setString("N");
				}
			}
		}

	}

	@Override
	public Object getResult(ResultGetter getter) throws SQLException {
		String str = getter.getString();
		if (str == null) {
			return null;
		} else if ("Y".equalsIgnoreCase(str)) {
			return new Boolean(true);
		} else {
			return new Boolean(false);
		}

	}

	@Override
	public Object valueOf(String s) {
		return s;
	}

}